package com.wn.servlet.demo03;

import javax.lang.model.element.VariableElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author yellow Docter
 * @date 2022 -06-28
 * @desc
 * ServletContext 读取资源:
 *
 * 01   web 下面的资源      a/b.txt
 *      src 下面的资源      c/d.txt
 *
 * 02   把项目部署到tomcat中,路径发生变化
 *      web下面的资源不会发生路径变化
 *      src下面的资源都会编译后存储到WEB-INF下面的classes目录下面
 *
 * 03 通过servletContext 读取根目录
 *     servletContext.getRealPath("/")  这个方法获取的是真实路径
 *     servletContext.getResourceAsStream("/a.txt"); 可以通过真实路径获取 文件的输入流
 *
 *     web下面的资源推荐使用: servletContext.getResourceAsStream("/a.txt") 获取流进行读取
 *
 *  04 Class.getResourceAsStream() 可以直接读取 classes下面的资源
 *     classes下面的资源通过 Class.getResourceAsStream()读取
 *
 *  总结:
 *    01  web下面的资源通过
 *        servletContext.getResourceAsStream("/a.txt") 读取
 *
 *    02 src下面的 资源编译后存储到classes下面通过
 *       XXXClass.getResourceAsStream("/a.txt") 读取
 *
 */
@WebServlet(urlPatterns = "/context04")
public class Context04Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 01拿到 servletcontext
        ServletContext servletContext = getServletContext();

        // C:\蜗牛\深圳java6期\javaweb_code\out\artifacts\day02_demo01_war_exploded
        // 快捷键
        // 选中代码   ctrl + Alt +M 可以把 代码块抽取成方法
//        test01(servletContext);

//        test02(servletContext);

        //通过 Class 这个类读取资源文件 ,可以读取classes 下面资源
        //  /       这个文件在classes的目录下面
        //  不加/    这个文件在classes的目录下面包下面的资源
        InputStream isC = Context04Servlet.class.getResourceAsStream("/c.txt");
        String msgC = readFile(isC);
        System.out.println("msgC = " + msgC);

        InputStream isD = Context04Servlet.class.getResourceAsStream("d.txt");
        String msgD = readFile(isD);
        System.out.println("msgD = " + msgD);
    }

    /**
     * 读取文件操作
     * @param servletContext
     */
    private void test02(ServletContext servletContext) {
        InputStream isA = servletContext.getResourceAsStream("/a.txt");
        String msgA = readFile(isA);
        System.out.println("msgA = " + msgA);

        InputStream isB = servletContext.getResourceAsStream("/WEB-INF/b.txt");
        String msgB = readFile(isB);
        System.out.println("msgB = " + msgB);

        InputStream isC = servletContext.getResourceAsStream("/WEB-INF/classes/c.txt");
        InputStream isD = servletContext.getResourceAsStream("/WEB-INF/classes/com/wn/servlet/demo03/d.txt");
        InputStream isDA = servletContext.getResourceAsStream("/WEB-INF/classes/com/wn/servlet/demo02/d.txt");
        String msgC = readFile(isC);
        System.out.println("msgC = " + msgC);
    }

    /**
     * 读取文件操作
     * @param servletContext
     * @throws FileNotFoundException
     */
    private void test01(ServletContext servletContext) throws FileNotFoundException {
        String realPath = servletContext.getRealPath("/");
        System.out.println("realPath = " + realPath);

        String aPath = servletContext.getRealPath("/a.txt");
        System.out.println("aPath = " + aPath);

        String msg  = readFile(aPath);
        System.out.println("msg = " + msg);

        String bPath = servletContext.getRealPath("/WEB-INF/b.txt");
        System.out.println("bPath = " + bPath);

        String msgB  = readFile(bPath);
        System.out.println("msgB = " + msgB);

        String cPath = servletContext.getRealPath("/WEB-INF/classes/c.txt");
        System.out.println("cPath = " + cPath);
        String msgC  = readFile(cPath);
        System.out.println("msgC = " + msgC);
    }

    /**
     * 文件的读取  通过字符串读取
     * @param aPath
     * @return
     */
    private String readFile(String aPath) throws FileNotFoundException {
        //io读取
        FileInputStream fis = new FileInputStream(aPath);
        return readFile(fis);
    }

    /**
     * 文件的读取  通过输入流读取
     * @param is
     * @return
     */
    public String readFile(InputStream is){

        // BufferedReader     字符缓存流
        //InputStreamReader   字符串 把输入流字节流转成 字符流
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = null;
        try {
            // BufferedReader 的新增方法  读取一行
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return s;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
